package com.web.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.web.annotation.CurrentUser;
import com.web.dao.UserDesignerMapper;
import com.web.dao.UserExpertMapper;
import com.web.dao.UserSupplyMapper;
import com.web.domain.Response;
import com.web.entity.*;
import com.web.service.ExpertsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@RestController
@RequestMapping(value = "/app")
public class ExpertsController {

    @Autowired
    private ExpertsService expertsService;
    @Resource
    private UserExpertMapper expertMapper;
    @Resource
    private UserDesignerMapper designerMapper;
    @Resource
    private UserSupplyMapper supplyMapper;
    /**
     * 专家列表
     * @param info
     * @return
     */
    @PostMapping("/experts/getExpertsList")
    public Response getExpertsList(@CurrentUser UserInfo info,Integer pageNum){
        if (pageNum == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        PageHelper.startPage(pageNum,10);
        List<ExpertsList> list = expertsService.getExpertsList(info.getUserId());
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }
    }


    /**
     * 专家详情
     * @param info
     * @param id
     * @return
     */
    @PostMapping("/experts/getExpertsDetails")
    public Response getExpertsDetails(@CurrentUser UserInfo info,Integer id){

        if (id == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        Map<String,Object> map = new HashMap<>();
        ExpertsDetails expertsDetails = expertsService.getExpertsDetails(id);
        map.put("details",expertsDetails);
        PageHelper.startPage(1,10);
        PageInfo pageInfo = new PageInfo(expertsService.getProjectExperience(expertsDetails.getUser_id()),10);
        if (pageInfo.getSize() == 0){
            map.put("comment",new List[0]);
        }else{
            if (pageInfo.getList().get(0) != null){
                map.put("comment",pageInfo.getList());
            }else{
                map.put("comment",new List[0]);
            }
        }
        return Response.custom().ok().addParams(map);
    }

    /**
     * 专家历史发布，经典案例
     * @param info
     * @param id
     * @return
     */
    @PostMapping("/experts/findExpertDetails")
    public Response findExpertDetails(@CurrentUser UserInfo info,Integer id,Integer type,Integer pageNum){
        if (id == null && type == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        if(type == 1){
            PageHelper.startPage(pageNum,10);
            List<ExpertRelease> list = expertsService.HistoricalRelease(id);
            PageInfo pageInfo = new PageInfo(list,10);
            if(pageNum > pageInfo.getPages()){
                return Response.custom().ok().addParams(new List[0]);
            }else{
                return Response.custom().ok().addParams(pageInfo.getList());
            }
        }else if(type == 2){
            PageHelper.startPage(pageNum,10);
            List<Case> list = expertsService.ClassicCase(id);
            PageInfo pageInfo = new PageInfo(list,10);
            System.out.println("====================================="+JSON.toJSONString(pageInfo));
            if(pageNum > pageInfo.getPages()){
                return Response.custom().ok().addParams(new List[0]);
            }else{
                if (pageInfo.getList().get(0) == null){
                    return Response.custom().ok().addParams(new List[0]);
                }
                return Response.custom().ok().addParams(pageInfo.getList());
            }

        }

        return Response.custom().failure("系统异常请联系管理员！");
    }


    /**
     * 添加关注记录
     * @param touser_id
     * @param type
     * @param userid
     */
    public void addConcernedExperts(String touser_id,int type,String userid){
        ConcernedExperts concernedExperts = new ConcernedExperts();
        concernedExperts.setTouser_id(touser_id);
        concernedExperts.setType(type);
        concernedExperts.setUser_id(userid);
        concernedExperts.setCreate_time(new Date());
        expertsService.insertConcernedExperts(concernedExperts);
    }
    /**
     * 关注
     * @param info
     * @return
     */
    @PostMapping("/experts/ConcernedExperts")
    public Response ConcernedExperts(@CurrentUser UserInfo info,ConcernedExperts concernedExperts){
        ConcernedExperts concernedExperts1 = expertsService.findMyConcernedExperts(info.getUserId(),concernedExperts.getTouser_id(),concernedExperts.getType());
        //关注类型(1.专家,2.工作室3.供应商,4.设计师,5.公司)
        if(concernedExperts1==null){
            if(concernedExperts.getType()==1){
                //专家
                UserExpert userExpert = expertMapper.findUserExpert("user_id",concernedExperts.getTouser_id());
                userExpert.setFans(userExpert.getFans()+1);
                userExpert.setModifyTime(new Date());
                expertMapper.updateByPrimaryKeySelective(userExpert);
                addConcernedExperts(concernedExperts.getTouser_id(),concernedExperts.getType(),info.getUserId());
                return Response.custom().ok();
            }else if(concernedExperts.getType()==2){
                //工作室
                UserDesigner userDesigner = designerMapper.findUserDesigner("user_id",concernedExperts.getTouser_id());
                userDesigner.setFans(userDesigner.getFans()+1);
                userDesigner.setModifyTime(new Date());
                designerMapper.updateByPrimaryKeySelective(userDesigner);
                addConcernedExperts(concernedExperts.getUser_id(),concernedExperts.getType(),info.getUserId());
                return Response.custom().ok();
            }else if(concernedExperts.getType()==3){
                //供应商
                addConcernedExperts(concernedExperts.getUser_id(),concernedExperts.getType(),info.getUserId());
                UserSupply userSupply = supplyMapper.findUserSupply("user_id",concernedExperts.getTouser_id());
                userSupply.setFans(userSupply.getFans()+1);
                userSupply.setModifyTime(new Date());
                supplyMapper.updateByPrimaryKeySelective(userSupply);
                return Response.custom().ok();
            }else
                addConcernedExperts(concernedExperts.getUser_id(),concernedExperts.getType(),info.getUserId());
                return Response.custom().ok();

        }
        return  Response.custom().failure("您已经关注过该专家！请勿重复关注!");
    }

    /**
     * 我的关注列表
     * @param info
     * @param pageNum
     * @return
     */
    @PostMapping("/experts/MyConcern")
    public Response MyConcern(@CurrentUser UserInfo info,Integer pageNum){
        if (pageNum == null){
            return Response.custom().failure("缺少请求参数,请重新核对后再提交!");
        }
        PageHelper.startPage(pageNum,10);
        List<MyExperts> list = expertsService.getMyConcern(info.getUserId());
        PageInfo pageInfo = new PageInfo(list,10);
        if(pageNum > pageInfo.getPages()){
            return Response.custom().ok().addParams(new List[0]);
        }else{
            return Response.custom().ok().addParams(pageInfo.getList());
        }

    }

    /**
     * 取消关注
     * @param info
     * @return
     */
    @PostMapping("/experts/RemoveConcerns")
    public Response RemoveConcerns(@CurrentUser UserInfo info,ConcernedExperts concernedExperts){
        ConcernedExperts concernedExperts1 = expertsService.findMyConcernedExperts(info.getUserId(),concernedExperts.getTouser_id(),concernedExperts.getType());
        if(concernedExperts1 != null){
            //关注类型(1.专家,2.工作室3.供应商,4.设计师,5.公司)
            if(concernedExperts.getType()==1){
                //取消关注专家
                UserExpert userExpert = expertMapper.findUserExpert("user_id",concernedExperts.getTouser_id());
                userExpert.setFans(userExpert.getFans()-1);
                userExpert.setModifyTime(new Date());
                expertMapper.updateByPrimaryKeySelective(userExpert);
                expertsService.RemoveConcerns(info.getUserId(),concernedExperts.getTouser_id(),concernedExperts.getType());
                return Response.custom().ok("成功取消关注！");
            }else if(concernedExperts.getType()==2){
                //取消关注工作室
                expertsService.RemoveConcerns(info.getUserId(),concernedExperts.getTouser_id(),concernedExperts.getType());
                UserDesigner userDesigner = designerMapper.findUserDesigner("user_id",concernedExperts.getTouser_id());
                userDesigner.setFans(userDesigner.getFans()-1);
                userDesigner.setModifyTime(new Date());
                designerMapper.updateByPrimaryKeySelective(userDesigner);
                return Response.custom().ok("成功取消关注！");
            }else if(concernedExperts.getType()==2){
                //取消关注供应商
                expertsService.RemoveConcerns(info.getUserId(),concernedExperts.getTouser_id(),concernedExperts.getType());
                UserSupply userSupply = supplyMapper.findUserSupply("user_id",concernedExperts.getTouser_id());
                userSupply.setFans(userSupply.getFans()-1);
                userSupply.setModifyTime(new Date());
                supplyMapper.updateByPrimaryKeySelective(userSupply);
                return Response.custom().ok("成功取消关注！");
            }  else
            expertsService.RemoveConcerns(info.getUserId(),concernedExperts.getTouser_id(),concernedExperts.getType());
            return Response.custom().ok("成功取消关注！");
        }
        return Response.custom().failure("该关注不存在！");
    }

}
